package cn.stylefeng.guns.modular.controller;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Clock;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.entity.IdjUser;
import cn.stylefeng.guns.modular.service.IIdjUserService;
import cn.stylefeng.guns.modular.util.JsonResult;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author TANGWJ
 * @since 2020-07-24
 */
@Controller
@RequestMapping("/modular/user")
public class IdjUserController extends BaseController{

	@Autowired
	private IIdjUserService service;

	private static String PREFIX = "/tbuser/";

	/**
	 * 跳转报告首页
	 */
	@RequestMapping
	public String index() {
		return PREFIX + "tbuser.html";
	}
	
	@RequestMapping("show")
	public String show(Long userId,ModelMap map) {
		map.put("userId", userId);
		return PREFIX + "tbuser_add.html";
	}

	 /**
     * 获取报告列表
     */
    @ResponseBody
    @RequestMapping("list")
    public LayuiPageInfo list(String userName, Integer userIdentity, String timeLimit) {
        //获取分页参数
        Page<IdjUser> page = LayuiPageFactory.defaultPage();
        //拼接查询条件
        String beginTime = null;
        String endTime = null;
        if (ToolUtil.isNotEmpty(timeLimit)) {
            String[] split = timeLimit.split(" - ");
            beginTime = split[0];
            endTime = split[1];
        }
        String company = null;
        //根据条件查询日志
        return service.getList(page, userName, userIdentity, company, beginTime, endTime);
    }
    
    @RequestMapping("details/{userId}")
    public ModelAndView add(@PathVariable Long userId) {
        ModelAndView index = service.edit(userId);
        index.setViewName(PREFIX + "tbuser_details.html");
        return index;
    }
    
    
    @RequestMapping("information")
    public ModelAndView information(String userName) {
        ModelAndView index = service.selectByKeyId(userName);
        index.setViewName(PREFIX + "tbuser_information.html");
        return index;
    }
    
	@RequestMapping("/userinfo")
	@ResponseBody
	public JsonResult userinfo(String userName){
		JsonResult rs = new JsonResult();
		rs.addContent("userinfo", service.userInfo(userName));
		return rs.declareSuccess();
	}
    
    @ResponseBody
    @RequestMapping("update")
    public ResponseData update(String userId, String status, String titieStatus, String recoveryTime) {
    	IdjUser user = new IdjUser();
    	  if (ToolUtil.isEmpty(userId)) {
              throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
          }
    	  user.setUserId(new Long(userId));
    	  if (ToolUtil.isNotEmpty(recoveryTime)) {
    		 try {
    		  	SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
				user.setRecoveryTime(formatter.parse(recoveryTime));
			 } catch (ParseException e) {
				e.printStackTrace();
			}
    		 user.setTitleStatus(titieStatus);
          }
    	  if (ToolUtil.isNotEmpty(status)) {
    		  user.setStatus(Integer.valueOf(status));
    	  }
    	  user.setUpdateTime(new Date());
    	  service.setStatus(user);
    	  return ResponseData.success();
    }
    
}

